package com.serkol.abmanager.component;
import com.alibaba.fastjson.JSONObject;

import com.serkol.abmanager.config.ConstantHeader;
import com.serkol.abmanager.entity.AdminToken;
import com.serkol.abmanager.entity.Users;
import com.serkol.abmanager.libraries.IpUtil;
import com.serkol.abmanager.service.UsersService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.i18n.SessionLocaleResolver;

import javax.servlet.annotation.WebFilter;
import javax.annotation.Resource;
import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Locale;

public class UsersFilter  {

    private String uploadhost;

    private TokenAdminUtils adminUtils;

    private UsersService service;



    private IpUtil ipUtil;

    public void init(FilterConfig filterConfig) throws ServletException {

    }
/*

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpServletResponse response = (HttpServletResponse) servletResponse;
        HttpServletRequest request = (HttpServletRequest) servletRequest;
        String type = request.getHeader("Content-Type");
        String method = request.getMethod();
        System.out.println("Ppppppppppppppp:" + method);
        if (method.equals("POST")) {
            String lang = request.getHeader("lang");
            request.getSession().setAttribute(SessionLocaleResolver.LOCALE_SESSION_ATTRIBUTE_NAME,lang);
            if(lang!=null) {
                setLocalP(lang);
            }

            //    LocaleContextHolder.setLocale();
            String appid = request.getHeader("appid");
            String terminal1 = request.getHeader("terminal");
            if (terminal1 == null) {
                terminal1 = "0";
            }
            int terminal = Integer.parseInt(terminal1);
            String ipAddr = ipUtil.getIpAddr(request);
            request.getSession().setAttribute("lang", lang);
            ConstantHeader.setLang(lang);
            ConstantHeader.setAppid(appid);
            ConstantHeader.setIp(ipAddr);
            if (terminal == 1) {
                String authorization = request.getHeader("Authorization");
                System.out.println("-----------------------------------authorization-------------------------------------");
                System.out.println(authorization);

                if (authorization != null) {
                    if (!authorization.trim().isEmpty()) {
                        System.out.println(authorization);
                        AdminToken admin = adminUtils.getAdmin(authorization);
                        System.out.println(admin);
                        if (admin != null) {
                            request.getSession().setAttribute("admin",admin);
                            ConstantHeader.setAdminToken(admin);

                        }else{

                        }
                    }
                }else{

                }
            }
            if (terminal == 3) {
                String authorization = request.getHeader("Authorization");
                if (authorization != null) {
                    if (!authorization.trim().isEmpty()) {
                        System.out.println(authorization);
                        Users byOpenId = service.findByOpenId(authorization);
                        System.out.println(byOpenId);
                        if (byOpenId != null) {
                            ConstantHeader.setUsers(byOpenId);

                        }else{


                        }
                    }else{

                    }
                }
            }
        }
    }
*/


    public void destroy() {

    }

    private void setLocalP(String localP){
        String[] s = localP.split("_");
        if(s.length>0){
            Locale locale = new Locale(s[0], s[1]);
            System.out.println("LANG:"+locale.toString());
            LocaleContextHolder.setDefaultLocale(locale);
            LocaleContextHolder.setLocale(locale);

            System.out.println("LLLLLLLLLLLLLLLLLLLL:"+LocaleContextHolder.getLocale().toString());
        }
    }
}
